package main

import (
	"fmt"
	"github.com/kardianos/service"
	"log"
	"os"
	"strings"
)

var logger service.Logger

type program struct{}

func (p *program) Start(service service.Service) error {
	go p.run()
	return nil
}

func (p *program) run() error {
	fmt.Println("hello world")
	return nil
}

func (p *program) Stop(s service.Service) error {
	if service.Interactive() {
		os.Exit(0)
	}
	return nil
}

func (p *program) Help() {
	fmt.Println("first-go\n\tusages:[start][stop][version]")
}

func main() {
	svcConfig := &service.Config{
		Name:        "First-go",
		DisplayName: "First-Go-Name",
		Description: "First Go DESC",
	}

	prg := &program{}
	action := "help"
	s, err := service.New(prg, svcConfig)
	if err != nil {
		log.Fatal(err)
	}
	if len(os.Args) > 1 {
		action = strings.ToLower(os.Args[1])
	}

	switch action {
	case "install":
		log.Println("install")
		err = service.Control(s, "install")
		if err != nil {
			log.Fatal(err)
		}
		return
	case "uninstall":
		log.Println("uninstall")
		err = service.Control(s, "install")
		if err != nil {
			log.Fatal(err)
		}
	case "start":
		log.Println("start")
		return
	case "stop":
		{
			log.Println("stop")
		}
	case "restart":
		{
			log.Println("restart")
		}
	case "status":
		{
			log.Println("status")
		}
	case "version":
		{
			log.Println("version")
		}
	default:
		{
			prg.Help()
		}
	}

	/*logger, err := s.Logger(nil)
	if err != nil {
		log.Fatal(err)
	}

	err = s.Run()
	if err != nil {
		logger.Error(err)
	}*/
}
